<template>
    <div>
        <div>
            <el-button class="mb-4" type="primary" @click="updatetem()">

                更新模版库
            </el-button>
        </div>
        <el-table size="large" :data="pager.lists">

            <el-table-column type="selection" width="55" />
            <el-table-column label="版本号" prop="user_version" min-width="100"></el-table-column>
            <el-table-column label="版本描述" prop="user_desc" min-width="250"></el-table-column>
            <el-table-column label="TemplateID" prop="template_id" min-width="120"></el-table-column>
            <el-table-column label="推送时间" prop="create_time" min-width="120"></el-table-column>
            <el-table-column label="启用" prop="is_black" min-width="100">
                <template #default="scope">
                    <el-switch v-model="scope.row.status" :active-value="1" :inactive-value="0"
                        style="margin-left: 5px;" @change="handleChange(scope.row.id, $event)" />
                </template>
            </el-table-column>
            <el-table-column label="操作" fixed="right" min-width="100">
                <template #default="{ row }">
                    <el-button type="primary" link @click="handledel(row.id)">
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="flex justify-end mt-4">
            <paginat v-model="pager" @change="getLists" />
        </div>
    </div>
</template>

<script lang="ts" setup>
import { getDraftList, delDraft, useTemplate, uploadDraftApi } from "@/api/application/chanel";
import { usePaging } from "@/hooks/usenew";
import feedback from "@/utils/feedback";
import { defineExpose } from 'vue';
import { ElLoading } from "element-plus";
const route = useRoute()
const formData = ref({
    app: route.query.app
})
//获取api
const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: getDraftList,
    params: formData.value,
});
getLists()

const updatetem = async () => {
    await feedback.confirm("确定要更新？");

    const loading = ElLoading.service({
        lock: true,
        text: 'Loading',
        background: 'rgba(0, 0, 0, 0.7)',
    })
    try {
        await uploadDraftApi({ app: route.query.app })
        getLists()
        loading.close()
    } catch (error) {
        loading.close()
    }


}

const handledel = async (id: any) => {
    await feedback.confirm("确定要删除？");
    await delDraft({ id: id });
    getLists()
}
const handleChange = async (id: any, row: any) => {
    try {
        await useTemplate({ status: row, id: id })
        getLists()
    } catch (error) {
        getLists()
    }
}

const someMethodInVersion = () => {
    getLists()
}
defineExpose({
    someMethodInVersion
})
</script>
